package edu.guigu.viewmodeldemo;

import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import edu.guigu.viewmodeldemo.databinding.ActivityViewModeDemoBinding;

public class ViewModeDemoActivity extends AppCompatActivity {

    private ActivityViewModeDemoBinding binding;
    private UserViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivityViewModeDemoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始ViewModel数据
        viewModel = new ViewModelProvider(this).get(UserViewModel.class);
        // 显示ViewModel数据
        binding.tvName.setText( viewModel.user.getName() );
        binding.tvAge.setText( viewModel.user.getAge() + "" );
    }
}